package com.matt.sooncleaner.wifiscan

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.net.wifi.ScanResult
import android.net.wifi.WifiManager
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BarUtils
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.matt.sooncleaner.R
import com.matt.sooncleaner.arch.ArchFragment
import com.matt.sooncleaner.arch.dp
import com.matt.sooncleaner.databinding.FragmentWifiScanResultBinding
import com.trustlook.wifisdk.wifiscan.WifiScanResult

class WifiScanResultFragment: ArchFragment<FragmentWifiScanResultBinding>() {
    private var mWifiScanResult: WifiScanResult? = null
    private lateinit var mAdapter: WifiResultAdapter
    private var isRisk = false

    override fun onInitView(binding: FragmentWifiScanResultBinding) {
        super.onInitView(binding)
        BarUtils.transparentStatusBar(requireActivity().window)
        BarUtils.setStatusBarLightMode(requireActivity(), false)
        binding.toolbar.updateLayoutParams<MarginLayoutParams> {
            topMargin = BarUtils.getStatusBarHeight()
        }
        binding.toolbar.setNavigationOnClickListener {
            requireActivity().finish()
        }
        mAdapter = WifiResultAdapter(mWifiScanResult?.wifiItemList?: mutableListOf())
        binding.resultList.addItemDecoration(MaterialDividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL).apply {
            dividerColor = Color.parseColor("#E0E0E0")
            val inset = 14.dp
            dividerInsetStart = inset
            dividerInsetEnd = inset
            isLastItemDecorated = false
        })
        binding.resultList.layoutManager = LinearLayoutManager(context)
        binding.resultList.adapter = mAdapter

        mWifiScanResult?.let {
            isRisk = it.isRisk()
            if (isRisk) {
                binding.wifiSecureStateImg.setImageResource(R.drawable.bg_wifi_result_risk)
                binding.wifiSecureStateIcon.setImageResource(R.drawable.ic_risk)
                binding.wifiSecureStateText.setText(R.string.risk)
            } else {
                binding.wifiSecureStateImg.setImageResource(R.drawable.bg_wifi_result_secure)
                binding.wifiSecureStateIcon.setImageResource(R.drawable.ic_secure)
                binding.wifiSecureStateText.setText(R.string.secure)
            }
            val ssid = it.ssid.replace("\"", "")
            val wifi5G = get5GWifiFlag(ssid)
            binding.wifiNameBtn.text = if (wifi5G.isNullOrEmpty()) ssid else "${ssid}_$wifi5G"
            binding.finishBtn.setOnClickListener {
                if (isRisk) {
                    startWifiSettings(requireContext())
                } else {
                    requireActivity().finish()
                }
            }
            binding.finishBtn.setText(if (!isRisk) R.string.done else R.string.disconnect_wifi)
        }
    }

    @SuppressLint("MissingPermission")
    private fun get5GWifiFlag(ssid: String): String? {
        val wifiManager = requireContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
        wifiManager.scanResults.forEach {
            if (ssid == it.SSID && is5GHz(it.frequency)) {
                return "5G"
            }
        }
        return null
    }

    private fun WifiScanResult.isRisk(): Boolean {
        return wifiItemList.firstOrNull { it.isSafe == 1 } != null
//        return true
    }

    fun setWifiScanResult(result: WifiScanResult) {
        mWifiScanResult = result
    }
}